/*
 * igo: com.cctv.igo.security.dto.Member.java Create On 2011-4-11 下午06:02:18 By Q-Wang
 * $Revision: 1.1 $
 */
package com.ctvit.framework.security.dto;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.ctvit.framework.dao.Condition;
import com.ctvit.framework.security.common.Copier;
import com.ctvit.framework.security.domain.PermAuthnMember;


/**
 * @author <a href="mailto:apeidou@gmail.com">Q-Wang</a>
 *
 */
public class Member extends PermAuthnMember implements UserDetails,CredentialsContainer {

	private Collection<GrantedAuthority> authorities;

	public Member() {
	}

	public static Member createMember(String mobile, String password) {
		return new Member(mobile, password);
	}
	public static Member createMember(PermAuthnMember member) {
		if (member instanceof Member) {
			return (Member) member;
		}
		return new Member(member);
	}
	public static Member createMember(PermAuthnMember member, Collection<GrantedAuthority> authorities) {
		if (member instanceof Member) {
			((Member)member).authorities = authorities;
			return (Member) member;
		}
		return new Member(member, authorities);
	}


	public Member(String mobile, String password) {
		setLocked(0L);
		setEnabled(1L);
		setExpired(0L);
		setPwdExpired(0L);

		setPassword(password);
		setPrincipal(mobile);
		setMobile(mobile);

		setCreator(null);
		setCreateTimestamp(new Date());

		authorities = Collections.emptyList();
	}

	private Member(PermAuthnMember member) {
		populate(member);
	}

	private Member(PermAuthnMember member, Collection<GrantedAuthority> authorities) {
		populate(member);
		this.authorities = authorities;
	}

	public void populate(PermAuthnMember member) {
		if (member==null) {
			return;
		}
		Copier.COPIER_MEMBER.copy(member, this, null);
	}

	public Condition createCondition(Condition condition) {
		if (condition==null) {
			condition = new Condition();
		}
		//		if (StringUtils.isNotEmpty(getUsername())) {
		//			condition.add("username", getUsername());
		//		}
		if (StringUtils.isNotEmpty(getEmail())) {
			condition.add("email",getEmail());
		}
		if (StringUtils.isNotEmpty(getMobile())) {
			condition.add("mobile",getMobile());
		}
		if (StringUtils.isNotEmpty(getPrincipal())) {
			condition.add("principal",getPrincipal());
		}
		if (StringUtils.isNotEmpty(getUserId2())) {
			condition.add("userId2",getUserId2());
		}
		return condition;
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
	 */
	public Collection<GrantedAuthority> getAuthorities() {
		return authorities;
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#getUsername()
	 */
	public String getUsername() {
		return getPrincipal();
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
	 */
	public boolean isAccountNonExpired() {
		return getExpired()!=1;
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
	 */
	public boolean isAccountNonLocked() {
		return getLocked()!=1;
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#isCredentialsNonExpired()
	 */
	public boolean isCredentialsNonExpired() {
		return getPwdExpired()!=1;
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#isEnabled()
	 */
	public boolean isEnabled() {
		//		return getEnabled()!=0;
		return true;
	}

	public void setAccountNonExpired(boolean accountNonExpired) {
		super.setExpired(accountNonExpired?0:1L);
	}
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled?1L:0);
	}
	public void setCredentialsNonExpired(boolean credentialsNonExpired) {
		super.setPwdExpired(credentialsNonExpired?0:1L);
	}
	public void setAccountNonLocked(boolean accountNonLocked) {
		super.setLocked(accountNonLocked?0:1L);
	}

	/**
	 * @see org.springframework.security.core.CredentialsContainer#eraseCredentials()
	 */
	public void eraseCredentials() {
		this.setPassword(null);
	}
}
